﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DAMS.Models;
using DAMS.Service;
using DAMS.Web.Areas.Manage.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace DAMS.Web.Controllers
{
    public class TestController : Controller
    {
        private readonly IRepository _repo;

        public TestController(IRepository repository)
        {
            this._repo = repository;
        }

        public IActionResult Index()
        {
            return View();
        }
        public IActionResult CategoryList(int type=0)
        {
            switch(type)
            {
                case 0:
                    return View(_repo.CategoryGetAll());
                default :
                    return View(_repo.CategoryGetTop());

            }
        }

        public IActionResult CategoryAdd()
        {
            IEnumerable<Category> tmp = _repo.CategoryGetAll();
 

            List<SelectListItem> sli = new List<SelectListItem>();
            sli.Add(new SelectListItem() { Text = "顶级分类", Value = "0" });
            foreach (Category item in tmp)
            {
                sli.Add(new SelectListItem()
                {
                    Text = "".PadLeft(item.Layer + 1, '　') + item.Name,
                    Value = item.ID.ToString()
                });
            }
            ViewBag.Category = sli;
            return View();
        }
        [HttpPost]
        public IActionResult CategoryAdd([FromForm]Category entity)
        {

            _repo.CategoryCreate(entity);
            return RedirectToAction("CategoryList");
        }

        public IActionResult CategoryDel(int id=-1)
        {
            if(id!=-1)
            {
                var result= _repo.CategoryDeleteByID(id);
                if (result.Succeeded)
                    return RedirectToAction("CategoryList");
                else
                {
                    string errmsg = "";
                    result.Error.ForEach(x => errmsg += x);
                    return Content(errmsg);
                }   
            }
            return Content("删除分类发生错误，错误的分类ID");
        }

        /// <summary>
        /// 查看分类明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult CategoryDetails(int id)
        {
            CategoryParentViewModel vm = new CategoryParentViewModel();
            vm.Entity = _repo.CategoryGetByID(id);
            vm.ParentList = _repo.CategoryGetParentListByID(id);
            vm.ChildList = _repo.CategoryGetChildListByID(id);
            return View(vm);
        }

        public IActionResult CategoryMove(int id,int newid)
        {
           var result=  _repo.CategoryMove(id, newid);
            return RedirectToAction("CategoryList");
        }
      
        public IActionResult TagAdd()
        {
            return View();
        }
        [HttpPost]
        public IActionResult TagAdd([FromForm]Tag tag)
        {
            _repo.TagCreate(tag);
            return View();
        }

        public IActionResult ArticleAdd()
        {
            Article a = new Article();
            a.Title = "文章1";
            a.PublishTime = DateTime.Now;
            a.CategoryID = 2;

           
            a.ArticleTags = new List<ArticleTag>();
            a.ArticleTags.Add(new ArticleTag() { Article = a,  Tag=new Tag() { TagName="河北省" } });
            a.ArticleTags.Add(new ArticleTag() { Article = a,  TagID=2 });
            _repo.ArticleCreate(a);

            return View();
        }

        public IActionResult Article(int id)
        {

            Article a=  _repo.ArticleGetByID(id);
            return Content("");
        }
        public IActionResult ArticleUp(int id)
        {
            Article a = _repo.ArticleGetByID(id);
            a.ArticleTags = new List<ArticleTag>();
            a.ArticleTags.Add(new ArticleTag() { Article = a, Tag = new Tag() { TagName = "kjlkj" } });
            _repo.ArticleUpdate(a);
            return Content("");
        }

        public IActionResult TagUp(string lowName,string newName)
        {
            Result<Tag> result= _repo.TagGet(lowName);
            result.Target.TagName = newName;
            _repo.TagUpdate(result.Target);
            return Content("");

        }
    }
}